package lijunyan;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Map;

public class JTableJPanel extends JScrollPane implements ListSelectionListener
{
    private final DefaultTableModel tableModel;
    private JTable jTable;
    private Map<String,String> translate;
    private String[] colomn;
    private ActionListener listener;

    public JTableJPanel()
    {
        this(null,null);
    }

    public JTableJPanel(ActionListener listener)
    {
        this(null,listener);
    }

    public JTableJPanel(Map<String,String> translate)
    {
        this(translate,null);
    }

    public JTableJPanel(Map<String,String> translate,ActionListener listener)
    {
        super();
        this.translate=translate;
        this.listener=listener;
        tableModel=new DefaultTableModel()
        {
            public boolean isCellEditable(int row,int column){return false;}
        };
        jTable=new JTable(this.tableModel);
        jTable.setFillsViewportHeight(true);
        jTable.getSelectionModel().addListSelectionListener(this);
//        jTable.addListSelectionListener
        this.setViewportView(jTable);

    }

    public void setListener(ActionListener listener)
    {
        this.listener=listener;
    }

    public void setTable(ResultSet resultSet) throws SQLException
    {
        tableModel.setRowCount(0);
        tableModel.setColumnCount(0);
        ResultSetMetaData rm=resultSet.getMetaData();
        int count=rm.getColumnCount();
        colomn=new String[count];
        for(int i=1;i<=count;++i)
        {
            String name=rm.getColumnLabel(i);
            colomn[i-1]=name;
            if(translate!=null)
            {
                String tmp=translate.get(name);
                if(tmp!=null)
                    name=tmp;
            }
            this.tableModel.addColumn(name);
        }
        while(resultSet.next())
        {
            Object[] result=new Object[count];
            for(int i=1;i<=count;++i)
                result[i-1]=resultSet.getObject(i);
            this.tableModel.addRow(result);
        }
    }

    public void valueChanged(ListSelectionEvent e)
    {
        if(e.getSource().equals(jTable.getSelectionModel()))
            if(this.listener!=null)
                this.listener.actionPerformed(new ActionEvent(this,1,"select"));
    }

    public String[] getColomn()
    {
        return this.colomn;
    }

    public Object[][] getSelected()
    {
        int[] rows=jTable.getSelectedRows();
        Object[][] ans=new Object[rows.length][colomn.length];
        for(int i=0;i<rows.length;++i)
            for(int j=0;j<colomn.length;++j)
                ans[i][j]=jTable.getValueAt(rows[i],j);
        return ans;
    }
}
